/**
 * 
 */
package com.zznode.client.wlanpolling.main;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.other.OthersActivity;

/**
 * @author Bean
 * 
 */
public class MainTabActivity extends TabActivity {
	private static String TAB_AP = "aplist";
	private static String TAB_SETTING = "setting";
	private static String TAB_OTHER = "other";

	private int[] tabInactiveRes = new int[] { R.drawable.menu_apselect,
			R.drawable.menu_setting, R.drawable.menu_other };

	private int[] tabActiveRes = new int[] { R.drawable.menu_apselect_active,
			R.drawable.menu_setting_active, R.drawable.menu_other_active };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintab);

		addTab("AP列表", TAB_AP, tabInactiveRes[0], APListActivity2.class);
		addTab("设置", TAB_SETTING, tabInactiveRes[1], SettingsActivity.class);
		addTab("其它", TAB_OTHER, tabInactiveRes[2], OthersActivity.class);

		getTabHost().setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				for (int i = 0; i < 3; i++) {
					View view = getTabHost().getTabWidget()
							.getChildTabViewAt(i);
					view.setBackgroundResource(0);

					if (view != getTabHost().getCurrentTabView()) {
						((ImageView) view.findViewById(R.id.icon))
								.setImageResource(tabInactiveRes[i]);
					} else {
						((ImageView) view.findViewById(R.id.icon))
								.setImageResource(tabActiveRes[i]);
					}
				}
				getTabHost().getCurrentTabView().setBackgroundResource(
						R.drawable.menu_activebg02);
			}
		});

		getTabHost().setCurrentTab(0);
	}
	
	public void changeTabByTabIndex(int index) {
		if(index > 2 || index <0)
			return;
		
		getTabHost().setCurrentTab(index);
	}

	private void addTab(String label, String tag, int drawableId, Class<?> c) {

		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec(tag);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(label);

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);

		if (tag.equals(TAB_AP)) {
			icon.setImageResource(tabActiveRes[0]);
			tabIndicator.setBackgroundResource(R.drawable.menu_activebg02);
		} else {
			tabIndicator.setBackgroundResource(0);
		}

		tabHost.addTab(spec);
	}

	@Override
	public void onBackPressed() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.exitconfirm)
               .setCancelable(false)
               .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                	   System.exit(0);
                   }
               })
               .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();
	}	
}
